package com.kun.service;

import com.kun.entity.Role;
import com.kun.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public int addRole(Role role) {
        if (role.getId()!=null){
            return roleMapper.updateRole(role);
        }else{
            return roleMapper.addRole(role);
        }

    }

    @Override
    public int deleteRole(int id) {
        return roleMapper.deleteRole(id);
    }

    @Override
    public int updateRole(Role role,Map map) {
        return roleMapper.updateRole(role);
    }

    @Override
    public List<Role> selectAllRoles(Map map) {
        if (map.containsKey("pageIndex")&&map.containsKey("pageSize")){
            Integer pageIndex = (Integer) map.get("pageIndex");
            Integer pageSize= (Integer) map.get("pageSize");
            pageIndex=pageSize*(pageIndex-1);
            map.replace("pageIndex",pageIndex);
        }
        return roleMapper.selectAllRoles(map);
    }
}
